package com.fs.groupSeven.controller;


import com.fs.groupSeven.pojo.Class;
import com.fs.groupSeven.pojo.Classroom;

import com.fs.groupSeven.service.ClassService;
import com.fs.groupSeven.service.ClassroomService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: fbb
 * @createDate: 2020/9/29 13:33
 */

@Controller
@RequestMapping("/classroom")
public class ClassroomController {
    @Autowired
    private ClassroomService classroomService;
    @Autowired
    private ClassService classService;

    @GetMapping("/classroomManage")
    public String getClassroomView() {
        //返回页面
        return "classroomManageFBB/classroomManagePage";
    }

    @GetMapping("/classroomManage1")
    public String getClassroomView1() {
        //返回页面
        return "classroomManageFBB/classroomManagePage";
    }

    @RequestMapping("/getClassroomList1")
    @ResponseBody
    public Map<String, Object> getClassroomList1() {
        List<Classroom> classrooms = classroomService.getClassroomList();

        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("message", "获取用户数据成功");
        result.put("roomList", classrooms);
        return result;
    }

    @RequestMapping("/getClassroomList")
    @ResponseBody
    public Map<String, Object> getClassroomList(Integer currentPage,
                                                Integer pageSize) {

        PageInfo<Classroom> classroomsByPage = classroomService.getClassroomsByPage(currentPage, pageSize);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("roomList", classroomsByPage.getList());

        result.put("message", "获取用户数据成功");
        result.put("totalPage", classroomsByPage.getPages());

        return result;
    }

    @RequestMapping("/getOneClassroom")
    @ResponseBody
    public Map<String, Object> getOneClassroom(Integer classroomId) {
        Classroom oneByClassroomId = classroomService.getOneByClassroomId(classroomId);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("message", "获取用户数据成功");
        result.put("classroom", oneByClassroomId);
        return result;
    }

    @RequestMapping("/editClassroom")
    @ResponseBody
    public Map<String, Object> editClassroom(@RequestBody Classroom classroom) {
        if (classroom.getClassroomId() == 0) {
            boolean bbb = classroomService.addOneRoom(classroom);
            Map<String, Object> result = new HashMap<>();
            if (bbb) {
                result.put("code", 0);
                result.put("message", "添加成功");
            } else {
                result.put("code", -1);
                result.put("message", "添加失败");
            }
            return result;
        } else {
            boolean insult = classroomService.updateByClassroom(classroom);
            Map<String, Object> result = new HashMap<>();
            if (insult) {
                result.put("code", 0);
                result.put("message", "修改成功");
            } else {
                result.put("code", -1);
                result.put("message", "修改失败");
            }
            return result;
        }
    }

    @RequestMapping("/getClassList")
    @ResponseBody
    public Map<String, Object> getClassList() {
        PageInfo<Class> classPageInfo = classService.queryclassAll(1, 2, 1);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("message", "获取成功");
        result.put("classList", classPageInfo.getList());
        return result;
    }


    @RequestMapping("/queryRoomByMessage")
    @ResponseBody
    public Map<String, Object> queryRoomByMessage(String roomMessage) {
        List<Classroom> classroomMessageLike = classroomService.getClassroomMessageLike(roomMessage);
        Map<String, Object> result = new HashMap<>();
        if (classroomMessageLike.size() == 0) {
            result.put("code", -1);
            result.put("message", "查无此教室");
        } else {
            result.put("code", 0);
            result.put("roomList", classroomMessageLike);
            result.put("message", "获取用户数据成功");
        }
        return result;
    }
}
